package com.rt.easyjava.common.email;

import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.utils.others.StringUtils;
import com.sun.mail.util.MailSSLSocketFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Slf4j
@Component
public class SendEmailComponent {

    @Value("${email.host}")
    public String emailHost;

    @Value("${email.port}")
    public String emailPort;

    @Value("${email.name}")
    public String emailName;

    @Value("${email.password}")
    public String emailPassword;


    public Result sendEmail(String email, String title, String content) {
        try {
            Properties properties = new Properties();
            properties.setProperty("mail.host",emailHost); // 邮件服务器地址
            properties.setProperty("mail.transport.protocol","smtp"); // 设置邮件发送协议
            properties.setProperty("mail.smtp.port", emailPort); // 端口
            properties.setProperty("mail.smtp.auth","false"); // 邮件服务身份验证
            if (emailHost.equals("smtp.qq.com")){
                properties.setProperty("mail.smtp.ssl.enable","true"); // 邮件ssl验证
            }
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.socketFactory", sf);
            //创建一个session对象
            Session session = Session.getDefaultInstance(properties);
            //开启debug模式
            session.setDebug(true);
            //获取连接对象
            Transport transport = session.getTransport();
            //连接服务器
            transport.connect(emailHost,emailName,emailPassword);
            //创建邮件对象
            MimeMessage mimeMessage = new MimeMessage(session);
            //邮件发送人
            mimeMessage.setFrom(new InternetAddress(emailName));
            //邮件接收人
            mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(email));
            //邮件标题
            mimeMessage.setSubject(title);
            //邮件内容
            mimeMessage.setContent(content,"text/html;charset=UTF-8");
            //发送邮件
            transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
            //关闭连接
            transport.close();
        } catch (Exception e) {
            log.error("错误日志" + e.getMessage(),e);
            return Result.builder().fault("错误日志" + e.getMessage());
        }
        return Result.builder();
    }

    public Result sendEmails(String sendToEmails, String copyToEmails, String subject, String content) {
        try {
            /*String email_host = parameterApi.findByName("Email_host").getValue();
            String email_port = parameterApi.findByName("Email_port").getValue();
            String email_name = parameterApi.findByName("Email_name").getValue();
            String email_password = parameterApi.findByName("Email_password").getValue();
            Properties properties = new Properties();
            properties.setProperty("mail.host",email_host); // 邮件服务器地址
            properties.setProperty("mail.transport.protocol","smtp"); // 设置邮件发送协议
            properties.setProperty("mail.smtp.port", email_port); // 端口
            properties.setProperty("mail.smtp.auth","false"); // 邮件服务身份验证
            if (email_host.equals("smtp.qq.com")){
                properties.setProperty("mail.smtp.ssl.enable","true"); // 邮件ssl验证
                String testEmail = parameterApi.findByName("TEST_EMAIL").getValue();
                vo.setEmail(StringUtils.isEmpty(testEmail) ? "2468800811@qq.com" : testEmail); // 测试环境，为了避免发送到生产上的邮箱中，默认一个邮箱号
                log.info("看下测试环境的邮箱发送清空" + vo);
            }*/

            Properties properties = new Properties();
            properties.setProperty("mail.host",emailHost); // 邮件服务器地址
            properties.setProperty("mail.transport.protocol","smtp"); // 设置邮件发送协议
            properties.setProperty("mail.smtp.port", emailPort); // 端口
            properties.setProperty("mail.smtp.auth","false"); // 邮件服务身份验证
            if (emailHost.equals("smtp.qq.com")){
                properties.setProperty("mail.smtp.ssl.enable","true"); // 邮件ssl验证
            }

            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.socketFactory", sf);

            //创建一个session对象
            Session session = Session.getDefaultInstance(properties);
            //开启debug模式
            session.setDebug(true);
            //获取连接对象
            Transport transport = session.getTransport();
            //连接服务器
            transport.connect(emailHost,emailName,emailPassword);
            //创建邮件对象
            MimeMessage mimeMessage = new MimeMessage(session);
            //邮件发送人
            mimeMessage.setFrom(new InternetAddress(emailName));

            //邮件接收人 将给定的以逗号分隔的地址序列解析为InternetAddress对象
            InternetAddress[] sendTo = InternetAddress.parse(sendToEmails);
            mimeMessage.setRecipients(Message.RecipientType.TO, sendTo);
            //mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(vo.getEmail()));
            // 抄送  将给定的以逗号分隔的地址序列解析为InternetAddress对象
            if(!StringUtils.isNull(copyToEmails)) {
                InternetAddress[] copyTo = InternetAddress.parse(copyToEmails);
                mimeMessage.setRecipients(Message.RecipientType.CC, copyTo);
            }
            //邮件标题
            mimeMessage.setSubject(subject);
            //邮件内容
            mimeMessage.setContent(content,"text/html;charset=UTF-8");
            //发送邮件
            transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
            //关闭连接
            transport.close();
        } catch (Exception e) {
            log.error("错误日志" + e.getMessage(),e);
            return Result.builder().fault("错误日志" + e.getMessage());
        }
        return Result.builder();
    }
}
